<template>
  <div>
    <SearchForm
        @search="onSearch"
        @reset="onReset"
        :search-list="searchList" :search-params="searchParams">
    </SearchForm>

    <div class="table-layout">
      <el-card shadow="never">
        <div class="table-handle">
          <el-button type="primary" size="mini" icon="el-icon-plus" @click="onAdd">新增比价记录</el-button>
          <el-button type="info" size="mini" icon="el-icon-download" @click="onDownloadTemplate">下载模版</el-button>
        </div>
        <div class="table-box">
          <el-table
              size="large"
              stripe
              border
              v-loading="loading"
              max-height="500px"
              :data="tableData"
              row-key="id"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255, 255, 255, 0.8)"
              @selection-change="onSelect"
          >
            <el-table-column prop="id" label="ID" width="60"/>
            <el-table-column prop="month" label="比价月份" width="120"/>
            <el-table-column prop="title" label="比价标题" min-width="200"/>
            <!-- <el-table-column prop="bidding_start_date" label="比价开始日期" width="150"/>
            <el-table-column prop="bidding_end_date" label="比价结束日期" width="150"/> -->
            <el-table-column prop="selected_supplier_id" label="入围供应商" width="150">
              <template slot-scope="scope">
                <span v-if="scope.row.supplier">{{ scope.row.supplier.name }}</span>
                <span v-else style="color: #999;">未选择</span>
              </template>
            </el-table-column>
            <el-table-column prop="status" label="状态" width="160">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.status === 1" type="primary">待导入官方参考价</el-tag>
                <el-tag v-if="scope.row.status === 2" type="warning">供应商报价中</el-tag>
                <el-tag v-if="scope.row.status === 3" type="success">已确定供应商</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="dishList.length" label="菜品数量" width="100">
              <template slot-scope="scope">
                <span v-if="scope.row.dishList && scope.row.dishList.length > 0" style="color: #409EFF; font-weight: bold;">
                  {{ scope.row.dishList.length }}
                </span>
                <span v-else style="color: #999;">0</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" fixed="right" width="280">
              <template slot-scope="scope">
                <el-button type="text" @click="onEdit(scope.row)">编辑</el-button>
                <el-button type="text" @click="onImportDishes(scope.row)" v-if="scope.row.biddingSupplierList.length === 0">导入官方参考价</el-button>
                <el-button type="text" @click="onManageSuppliers(scope.row)" v-if="scope.row.dishList.length > 0">管理供应商</el-button>
                <el-button type="text" @click="onViewQuotes(scope.row)" v-if="scope.row.dishList.length > 0">管理供应商报价</el-button>
                <el-button type="text" @click="showDishList(scope.row)" v-if="scope.row.dishList.length > 0">查看官方参考价</el-button>
                <el-button type="text" style="color: #F82222" @click="onDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-card>
      <div class="page-box">
        <el-pagination
            background
            :current-page="pageNo"
            :page-sizes="pageSizes"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalSize"
            @size-change="changePageSize"
            @current-change="changePageNo"
        />
      </div>
    </div>
    <Add ref="addFormRef" @get-page="initData" />
    <QuoteList ref="quoteListRef" @supplier-selected="onSupplierSelected" />
    <SelectSupplier ref="selectSupplierRef" @get-page="initData" />
    <ManageSuppliers ref="manageSuppliersRef" @get-page="initData" />
    <ImportDishes ref="importDishesRef" @get-page="initData" />
    <DishList ref="dishListRef" @dish-added="onDishAdded" />
  </div>
</template>
<script>

import crud from "@/mixins/crud";
import { http } from "@/api";
import { downloadDishPriceTemplate } from '@/utils/excelTemplate';
import Add from "./add";
import QuoteList from "./quoteList";
import SelectSupplier from "./selectSupplier";
import ManageSuppliers from "./manageSuppliers";
import ImportDishes from "./importDishes";
import DishList from "./dishList";

export default {
  name: "MonthlyBiddingIndex",
  components: {
    Add,
    QuoteList,
    SelectSupplier,
    ManageSuppliers,
    ImportDishes,
    DishList
  },
  mixins: [crud],
  data() {
    return {
      request: http.monthlyBidding,
      searchList: [
        {
          label: '比价月份',
          prop: 'month',
          name: 'input',
          placeholder: '请输入比价月份 YYYY-MM'
        },
        {
          label: '比价标题',
          prop: 'title',
          name: 'input',
          placeholder: '请输入比价标题'
        },
        {
          label: '状态',
          prop: 'status',
          name: 'select',
          options: [
            { value: 1, label: '进行中' },
            { value: 2, label: '已结束' },
            { value: 3, label: '已确定供应商' }
          ]
        }
      ],
      searchParams: {
        month: '',
        title: '',
        status: ''
      },
      defaultParams: {
      },
      tableData: []
    }
  },
  methods: {
    onViewQuotes(row) {
      this.$refs.quoteListRef.open(row);
    },
    onSelectSupplier(row) {
      this.$refs.selectSupplierRef.open(row);
    },
    onManageSuppliers(row) {
      this.$refs.manageSuppliersRef.open(row);
    },
    onImportDishes(row) {
      this.$refs.importDishesRef.open(row);
    },
    async onDownloadTemplate() {
      try {
        await downloadDishPriceTemplate();
        this.$message.success('模版下载成功');
      } catch (error) {
        console.error('下载模版失败:', error);
        this.$message.error('模版下载失败: ' + error.message);
      }
    },
    showDishList(biddingInfo) {
      this.$refs.dishListRef.open(biddingInfo);
    },

    onSupplierSelected(biddingInfo) {
      // 更新本地数据
      const index = this.tableData.findIndex(item => item.id === biddingInfo.id);
      if (index !== -1) {
        this.tableData[index] = { ...this.tableData[index], ...biddingInfo };
      }
      this.$message.success('入围供应商已更新');
    },

    onDishAdded(biddingInfo) {
      // 更新本地数据
      const index = this.tableData.findIndex(item => item.id === biddingInfo.id);
      if (index !== -1) {
        this.tableData[index] = { ...this.tableData[index], ...biddingInfo };
      }
      // this.$message.success('菜品已增加，请通知供应商重新报价');
    }
  }
}
</script>

<style scoped lang="scss">
.table-layout {
  margin-top: 20px;
}
.table-handle {
  margin-bottom: 20px;
}
.page-box {
  margin-top: 20px;
  text-align: right;
}
</style>
